package com.offcn.pay;

import com.offcn.interceptor.FeignInterceptor;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * @Author: zola
 * @Date: 2021/9/23 - 09 - 23 - 22:07
 * @Description: com.offcn.pay
 * @Version: 1.0
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.offcn.feign")
public class PayServiceApplication {

    // 环境变量对象
    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(PayServiceApplication.class,args);
    }

    @Bean
    public FeignInterceptor feignInterceptor(){
        return new FeignInterceptor();
    }

    // 队列
    @Bean
    public Queue createQueue(){
        return new Queue(environment.getProperty("mq.pay.queue.seckillorder"));
    }

    // 交换机
    @Bean
    public DirectExchange seckillExchange(){
        return new DirectExchange(environment.getProperty("mq.pay.exchange.seckillorder"));
    }

    // 队列和交换机的绑定
    @Bean
    public Binding seckillBinding(){
        return BindingBuilder.bind(createQueue()).to(seckillExchange()).with(environment.getProperty("mq.pay.routing.seckillkey"));
    }
}
